/**
 * 
 */
package co.fitstart.mobile.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.UserAddress;
import co.fitstart.mobile.dao.support.UserAddressDaoSupport;
import co.fitstart.mobile.service.UserAddressService;

/**
 * @author Guangli Yan
 *
 */
@Service("userAddressService")
public class UserAddressServiceImpl implements UserAddressService{

	@Autowired
	private UserAddressDaoSupport userAddressDaoSupport;
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#listUserAddress(co.fitstart.entity.user.User)
	 */
	public List<UserAddress> listUserAddress(User user) {

		List<UserAddress> userAddresses = userAddressDaoSupport.list(user);
		
		return userAddresses;
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#validateUserAddress(java.lang.Long)
	 */
	public void validateUserAddress(Long addressId) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#loadUserDefaultAddress(co.fitstart.entity.user.User)
	 */
	public UserAddress loadUserDefaultAddress(User user) {

		UserAddress userDefaultAddress = userAddressDaoSupport.loadUserDefaultAddress(user);
		
		if (userDefaultAddress == null) {
			
			List<UserAddress> list = userAddressDaoSupport.list(user);
			
			if (list.size() > 0) {
				
				userDefaultAddress = list.get(0);
				
			}
		}
		return userDefaultAddress;
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#load(java.lang.Long)
	 */
	public UserAddress load(Long id) {

		UserAddress userAddress = genericDaoSupport.load(UserAddress.class, id);
		
		return userAddress;
	}



	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#create(co.fitstart.entity.user.User, co.fitstart.entity.user.UserAddress)
	 */
	public Integer create(User user, UserAddress userAddress) {

		List<UserAddress> list = userAddressDaoSupport.list(user);
		
		if (list.size() <= 0) {
			
			userAddress.makeDefault();
		}
		if (userAddress.isDefaultAddress()) {
			
			for (UserAddress address : list) {
				
				address.defaultAddress(false);
			}
		}
		userAddress.create(user);

		Long id = (Long) genericDaoSupport.save(userAddress);
		
		return id.intValue();
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#delete(java.lang.Long)
	 */
	public void delete(Long id) {
		
		UserAddress userAddress = genericDaoSupport.load(UserAddress.class, id);
		
		userAddress.delete();
		
		genericDaoSupport.update(userAddress);
		
		// 删除默认地址
		if (userAddress.isDefaultAddress()) {
			
			List<UserAddress> userAddresses = userAddressDaoSupport.list(userAddress.getUser());
			
			if (userAddresses.size() > 0) {
				// 时间倒序第一个地址设置成默认地址
				userAddresses.get(0).makeDefault();
				
				genericDaoSupport.update(userAddresses.get(0));
			}
		}
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#update(co.fitstart.entity.user.UserAddress)
	 */
	public void update(UserAddress address) {
		
		genericDaoSupport.update(address);
		
	}

	public void edit(User user, UserAddress address) {
		
		if (address.isDefaultAddress()) {
			
			List<UserAddress> addresses = userAddressDaoSupport.list(user);
			
			for (UserAddress userAddress : addresses) {
				
				userAddress.defaultAddress(false);
				
				genericDaoSupport.update(userAddress);
			}
			address.makeDefault();
		}
		genericDaoSupport.update(address);
	}

	/* (non-Javadoc)
	 * @see co.fitstart.mobile.service.UserAddressService#pretermit(co.fitstart.entity.user.User, java.lang.Long)
	 */
	public void pretermit(User user, Long id) {

		List<UserAddress> list = userAddressDaoSupport.list(user);
		
		for (UserAddress userAddress : list) {
			
			userAddress.defaultAddress(false);
			
			if (userAddress.getId().equals(id)) {
				userAddress.makeDefault();
			}
				
			genericDaoSupport.update(userAddress);
		}
		UserAddress userAddress = genericDaoSupport.load(UserAddress.class, id);
		
		userAddress.makeDefault();
		
		genericDaoSupport.update(userAddress);
		
	}

}
